package com.hugoo.core.scheduling;

import com.hugoo.core.Conf;
import com.hugoo.core.util.TL;
import com.hugoo.sch.ExecProc;
import com.hugoo.sch.FtpDownload;
import it.sauronsoftware.cron4j.SchedulingPattern;
import it.sauronsoftware.cron4j.Task;
import it.sauronsoftware.cron4j.TaskCollector;
import it.sauronsoftware.cron4j.TaskTable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public class RptTaskCollector implements TaskCollector {

    private final static Log log = LogFactory.getLog(RptTaskCollector.class);

    public TaskTable getTasks() {
        String schs = Conf.getBaseProp(Conf.SCH._schs);
        if (TL.isEmpty(schs)) {
            return new TaskTable();
        } else {
            String schz[] = schs.split(","), schc, scht;
            TaskTable ret = new TaskTable();
            SchedulingPattern pattern;
            Task tsk = null;
            for (String sch : schz) {
                schc = Conf.getBaseProp(sch, Conf.SCH._sch_cycle);
                scht = Conf.getBaseProp(sch, Conf.SCH._sch_oper_type);
                if (TL.isEmpty(schc, scht)) {
                    log.info("调度周期【" + schc + "】、调度类型【" + scht + "】配置无效=^|0|^=");
                    continue;
                }
                pattern = new SchedulingPattern(schc);
                if ("FTP_UPLOAD".equalsIgnoreCase(scht)) {
                } else if ("FTP_DOWNLOAD".equalsIgnoreCase(scht)) {
                    tsk = new FtpDownload(sch);
                } else if ("EXEC_PROC".equalsIgnoreCase(scht)) {
                    //
                    tsk = new ExecProc(sch);
                } else {
                    log.warn("调度周期【" + schc + "】、调度类型【" + scht + "】配置无效=^|0|^=");
                    continue;
                }
                ret.add(pattern, tsk);
            }
            return ret;
        }
    }
}
